﻿using System;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;

namespace YArchitech.Temp
{
	public class HYDuctStraightRow : DataGridViewRow
	{
		public HYDuctStraight MEPObject
		{
			get
			{
				return this.m_Straight;
			}
		}

		public int AttachId
		{
			get
			{
				return this.m_AttachId;
			}
		}

		public HYDuctStraightRow(DataGridView grid, HYDuctStraight strt)
		{
			this.m_GridView = grid;
			base.CreateCells(grid);
			this.m_Straight = strt;
			this.m_AttachId = this.MEPObject.Id;
			base.Tag = this.m_AttachId;
			this.Binding();
		}

		public void Refresh()
		{
			this.Binding();
		}

		private void Binding()
		{
			this.GetCell("Index").SetValue(this.MEPObject.Index);
			this.GetCell("Section").SetValue(this.MEPObject.ShapeString);
			DataGridViewCell cell = this.GetCell("Flow");
			cell.Style.BackColor = HYDuctStraightRow.colorEdit;
			cell.SetValue(this.MEPObject.Flow * 3600.0, 3);
			DataGridViewCell cell2 = this.GetCell("Width");
			cell2.Style.BackColor = HYDuctStraightRow.colorEdit;
			cell2.SetValue(this.MEPObject.Width);
			if (this.MEPObject.Shape != HYShapeProfile.Round)
			{
				DataGridViewCell cell3 = this.GetCell("Height");
				cell3.Style.BackColor = HYDuctStraightRow.colorEdit;
				cell3.SetValue(this.MEPObject.Height);
			}
			this.GetCell("Velocity").SetValue(this.MEPObject.Velocity, 3);
			this.GetCell("Length").SetValue(this.MEPObject.Length, 3);
			this.GetCell("R").SetValue(this.MEPObject.Rm, 3);
			this.GetCell("PL").SetValue(this.MEPObject.FrictionalResistance, 3);
			DataGridViewCell cell4 = this.GetCell("Cofficient");
			cell4.Style.BackColor = HYDuctStraightRow.colorEdit;
			cell4.SetValue(this.MEPObject.Coefficient, 3);
			this.GetCell("PJ").SetValue(this.MEPObject.ShockResistance, 3);
			this.GetCell("P").SetValue(this.MEPObject.Resistance, 3);
		}

		public DataGridViewCell FlowCell
		{
			get
			{
				if (this.m_FlowCell == null)
				{
					this.m_FlowCell = this.GetCell("Flow");
				}
				return this.m_FlowCell;
			}
		}

		public double GetFlow()
		{
			return this.FlowCell.GetDoubleValue();
		}

		private DataGridViewCell GetCell(string colName)
		{
			int index = this.m_GridView.Columns[colName].Index;
			return base.Cells[index];
		}

		public bool OnCellValueChange(string colName, bool beModified)
		{
			DataGridViewCell cell = this.GetCell(colName);
			if (colName == "Width")
			{
				this.MEPObject.Width = cell.GetIntValue();
				return true;
			}
			if (colName == "Height")
			{
				this.MEPObject.Height = cell.GetIntValue();
				return true;
			}
			if (!(colName == "Flow") || !beModified)
			{
				return false;
			}
			double num = this.MEPObject.Flow - this.MEPObject.AddFlow;
			double addFlow = this.MEPObject.AddFlow;
			double num2 = this.GetFlow() / 3600.0;
			if (num2 - num >= addFlow)
			{
				this.MEPObject.AddFlow = num2 - num;
				this.MEPObject.Flow = num2 - this.MEPObject.AddFlow;
				this.MEPObject.ReAllocateFlow();
			}
			if (num2 - num < addFlow)
			{
				YJKMessageBox.Show("输入流量不能小于前一根风管的流量！");
				cell.SetValue(this.MEPObject.Flow * 3600.0, 3);
				return false;
			}
			return true;
		}

		private static Color colorEdit = Color.FromArgb(255, 255, 200);

		private HYDuctStraight m_Straight;

		private DataGridView m_GridView;

		private int m_AttachId;

		private DataGridViewCell m_FlowCell;
	}
}
